package com.enniswu.weitter.widget;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.enniswu.weitter.R;

public class LoadingDialog extends DialogFragment implements DialogInterface.OnKeyListener {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().setOnKeyListener(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        setCancelable(false);
        return new AlertDialog.Builder(getContext(), R.style.TransparentDialog).setView(R.layout.dialog_loading).create();
    }

    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (keyCode != KeyEvent.KEYCODE_BACK){
            return false;
        }
        if (isVisible()) {
            dismiss();
            if (getActivity() != null)
                getActivity().finish();
            return true;
        }
        return false;
    }
}
